	.data
prompt: .asciiz "Only runs with -mapped_io flag.\nPlease type 6 input lines:\n"
nl:	.asciiz "\n"
row: .asciiz "  +---+---+---+---+---+---+---+---+\n"
bottom: .asciiz "    a   b   c   d   e   f   g   h  \n"
bar: .asciiz "|"
empty: .asciiz "   "
space: .asciiz " "
dot: .asciiz ":"
clear: .asciiz "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

bpawn: .asciiz "p"
bknig: .asciiz "h"
bbish: .asciiz "b"
brook: .asciiz "r"
bquee: .asciiz "q"
bking: .asciiz "k"

wpawn: .asciiz "P"
wknig: .asciiz "H"
wbish: .asciiz "B"
wrook: .asciiz "R"
wquee: .asciiz "Q"
wking: .asciiz "K"

A:		.asciiz "A"
B:		.asciiz "B"
C:		.asciiz "C"
D:		.asciiz "D"
E:		.asciiz "E"
F:		.asciiz "F"
G:		.asciiz "G"
H:		.asciiz "H"

uparr: .asciiz "^"
leftarr: .asciiz "<"
select: .asciiz "Selected: "
hitenter: .asciiz "Hit enter to select piece."
selectempty: .asciiz "Cannot select empty space."
invalid:	.asciiz "Invalid move.\n"
moveto:	.asciiz "Move to: "
wturn:	.asciiz "White's turn. "
bturn:	.asciiz "Black's turn. "
cma:	.asciiz ","
error:	.word 0
turn:	.word 1
# Create the chessboard array (see wiki)
board: .word 1110795044, 286331153, 0, 0, 0, 0, -1717986919, -893523028
nwcap: .word 0
nbcap: .word 0
wcaptured: .word 0,0,0,0,0,0,0,0
bcaptured: .word 0,0,0,0,0,0,0,0

# The current location x=column, y=row
locx: .word 0
locy: .word 7

# The selected location x=column, y=row
slocx: .word -1
slocy: .word -1

	.text
	.globl main
main:

#
# Draw the chess board
#
drawboard:
	jal		newline
	la		$a0,board
	li		$a1,1
	jal		ai_get_owned_value
	add		$a0,$zero,$v0
	li		$v0,1
	syscall
	jal		newline
	li 		$s2, 8
	li 		$s3, 0
board_row_loop:		# for (int s0 = 0; s0 < 8; s0++) :
	la 		$a0, row
	jal 	printstr
	
	li		$a0, 8
	subu	$a0, $a0, $s3
	jal 	printint	# draw row number
	
	la 		$a0, space
	jal 	printstr
	
	li 		$s0, 8
	li 		$s1, 0
board_col_loop:		# for (int s1 = 0; s1 < 8; s1++) :
	
	la 		$a0, bar
	jal 	printstr
	
drawchesspiece:
	move 	$a0, $s1
	move 	$a1, $s3
	jal 	drawpiece			# draw the chess piece

donedrawchesspiece:

	addi 	$s1, $s1, 1
	
	bne 	$s1, $s0, board_col_loop	
endboard_col_loop:	# endfor;

	la 		$a0, bar
	jal 	printstr
	la		$a0, space
	jal		printstr
	lw		$t0, locy
	beq		$t0, $s3, draw_locy
	j		not_locy
	
draw_locy:
	la		$a0, leftarr
	jal		printstr
	
not_locy:
	jal 	newline
	
	addi 	$s3, $s3, 1
	bne 	$s3, $s2, board_row_loop
end_board_row_loop:	# endfor;

	la 		$a0, row
	jal 	printstr
	
	la 		$a0, bottom
	jal 	printstr				# draw column letter
	
	
	lw		$s1, locx
	li		$s0, 0
draw_locx:
	la		$a0, space
	jal		printstr
	jal		printstr
	jal		printstr
	jal		printstr
	
	addi	$s0, $s0, 1
	
	ble		$s0, $s1, draw_locx
	
	la		$a0, uparr
	jal		printstr
	
	jal		newline
	
	lw		$a0, turn
	beq		$a0, $zero, drawbturn
drawwturn:
	la		$a0,board
	li		$a1,1
	jal		ai_get_owned_value
	add		$a0,$zero,$v0
	li		$v0,1
	syscall
	jal		newline
	
	la		$a0, wturn
	jal		printstr
	j		isselected
drawbturn:	
	la		$a0,board
	li		$a1,0
	jal		ai_get_owned_value
	add		$a0,$zero,$v0
	li		$v0,1
	syscall
	jal		newline
	
	la		$a0, bturn
	jal		printstr
	
isselected:
	lw		$a0, slocx
	blt		$a0, $zero, notselected
	lw		$a1, slocy
	blt		$a0, $zero, notselected
	
#	jal		getpiece
#	beq		$v0, $zero, nopiece
	
	la		$a2,board
	jal		ai_gen_moves
	move	$a0,$v0
	add		$v0,$zero,1
	syscall
	la		$a1,board
	li		$a0,0
	jal		ai_check_check
	la		$a1,board
	li		$a0,1
	jal		ai_check_check
	jal		newline
	lw		$a0,slocx
	lw		$a1,slocy

	la		$a0, select
	jal		printstr
	
	lw		$a0, slocx
	li		$a1, 2
	multu	$a0, $a1
	mflo	$a0
	
	la		$a1, A
	add		$a0, $a0, $a1
	jal		printstr
	
	lw		$a0, slocy
	li		$a1, 8
	sub		$a0, $a1, $a0
	jal		printint
	
	jal		newline
	la		$a0, moveto
	jal		printstr
	
	lw		$a0, locx
	lw		$a1, locy
	jal		printboardloc

	j		finishdrawboard
notselected:
	la		$a0, hitenter
	jal		printstr
	j		finishdrawboard
#nopiece:
#	la		$a0, selectempty
#	jal		printstr
	
finishdrawboard:

	jal		newline
	lw		$a0, error
	beq		$a0, 1, invalidmove
	j		noerror
invalidmove:
	la		$a0, invalid
	jal		printstr
noerror:
	sw		$zero, error

#
# Finished drawing the chessboard
#

# Register usage:
#		s0		loop counter
#		t0		address of recv_ctrl
#		t1		address of recv_buffer
#		t2		address of trans_ctrl
#		t3		address of trans_buffer
#		t4, t5		temporaries
#		t6		char read from input
#		t7		1 => char in t6

	li 		$s0, 3		# loop counter


	li 		$t0, 0xffff0000	# recv ctrl
	li 		$t1, 0xffff0004	# recv buf
	li 		$t2, 0xffff0008	# trans ctrl
	li 		$t3, 0xffff000c	# trans buf

# First, read and echo 3 lines of input by polling the IO registers, not through
# interrupts:

	mtc0 	$0, $12				# Clear IE bit in Status reg to disable interrupts

l1:
	lw 		$t4, 0($t0)			# Wait for receiver ready
	and 	$t4, $t4, 1
	beq 	$t4, 0, l1

	lw 		$t6, 0($t1)			# Read character

l2:
	lw 		$t4, 0($t2)			# Wait for transmitter ready
	and 	$t4, $t4, 1
	beq 	$t4, 0, l2

	#sw 	$t6, 0($t3)			# Write character
	
	la		$a0, clear			# Clear the screen
	jal		printstr
	
	li		$s0, 100			# D is right
	beq		$t6, $s0, checkright
	
	li		$s0, 97
	beq		$t6, $s0, checkleft # A is left
	
	li		$s0, 119
	beq		$t6, $s0, checkup	# W is up
	
	li		$s0, 115
	beq		$t6, $s0, checkdown	# S is down
	
	beq 	$t6, 0xa, enter		# Enter to select a piece
	
	j	drawboard				# Other keys will be ignored (to be modified)


	enter:								# Enter has been pressed
		lw		$s0, slocx
		blt		$s0, $zero, selectloc	# If slocx<0 then a selection has not been made yet
		lw		$s0, slocy
		blt		$s0, $zero, selectloc	# If slocy<0 then a selection has not been made yet
	
									# A selection has been made
		jal		checkmove			# Check if the move is valid from (slocx,slocy) => (locx, locy)
		j		drawboard			# And perform the move
		
	selectloc:						# A selection has not been made yet
		lw		$s0, locx
		sw		$s0, slocx			# Move locx -> slocx
		lw		$s0, locy
		sw		$s0, slocy			# Move locy -> slocy
		j		drawboard

	checkright:						# Right has been pressed
		lw		$s0, locx
		li		$s1, 6
		ble		$s0, $s1, right		# Check that locx+1 <= 7
		j		drawboard
	right:
		addi	$s0, $s0, 1			# Then locx++
		sw		$s0, locx
		j		drawboard

	checkleft:						# Left has been pressed
		lw		$s0, locx
		bgt		$s0, $zero, left	# Check that locx-1 > 0
		j		drawboard
	left:
		addi	$s0, $s0, -1		# Then locx--
		sw		$s0, locx
		j		drawboard

	checkup:						# Up has been pressed
		lw		$s0, locy
		bgt		$s0, $zero, up		# Check that locy-1 > 0
		j		drawboard			# Top-Left is our origin.
	up:								# Up corresponds to decrementing locy
		addi	$s0, $s0, -1
		sw		$s0, locy			# Then locy--
		j		drawboard

	checkdown:						# Down has been pressed
		lw		$s0, locy
		li		$s1, 6
		ble		$s0, $s1, down		# Check that locy+1 <= 7
		j		drawboard
	down:
		addi	$s0, $s0, 1			# Then locy++
		sw		$s0, locy
		
	# Loop program infinitely	
	j		drawboard	

	# Done echoing, so terminate program.
	li $v0 10
	syscall			# syscall 10 (exit)

	# Return from handler.
ret_handler:
	mfc0 $t4 $12		# Enable interrupts and mask in Status reg
	ori $t4 $t4 0xff01
	mtc0 $t4 $12

	eret			# return to interrupted instruction


exception:
	li $v0 4		# Non-interrupt exception
	la $a0 other_str	# Print message and ignore
	syscall
	b ret_handler

no_recv_ready:
	li $v0 4		# Receiver was not ready after interrupt
	la $a0 no_recv_str	# Print message and ignore
	syscall
	b ret_handler

bad_int:
	li $v0 4		# Interrupt was not from recv or trans
	la $a0 bad_int_str	# Print message and ignore
	syscall
	b ret_handler

no_trans_ready:
	li $v0 4		# Transmitter was not ready after interrupt
	la $a0 no_trans_str	# Print message and ignore
	syscall
	b ret_handler

#############################################################################
printint:						#	this routine makes it easier to print an integer
								#	though the same preparations must be made as before
		li		$v0, 1			#	prepare for system call
		syscall

		jr		$ra				#	return to caller

printstr:						#	this routine makes it easier to print strings
		li		$v0, 4			#	prepare for system call
		syscall

		jr		$ra				#	return to caller

newline:						#	this routine makes it easier to print "\n"
		li		$v0, 4			#	prepare for system call
		la		$a0, nl
		syscall

		jr		$ra				#	return to caller
		
mod:							#	calculates $a0 mod $a1
		divu	$a0, $a1
		mfhi	$v0

		jr		$ra
terminate:
		li		$v0, 10
		syscall
#############################################################################
# Chessboard utility routines
#

##
# Get piece for column and row
# $a0 - column x
# $a1 - row y
# $a2 - chess board address
# $v0 - piece(x,y)
       .text
        .globl  getpiece
getpiece:
                                # prolog        
        sub     $sp,$sp,4       #   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       #   Push callers frame pointer
        sw      $fp,($sp)
        sub     $fp,$sp,0       #   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp         #   $sp = $fp
		
		sub		$v0,$zero,1		#	initialize the return value
		blt		$a0,0,getpieceD
		blt		$a1,0,getpieceD
		bgt		$a0,7,getpieceD	#	return 0 if out of bounds
		bgt		$a1,7,getpieceD	#	"                       "
		move	$t0, $a0		#	column x
		move	$t1, $a1		#	row y
		
		move	$t2, $a2		#	get address to the chess board
		
		li		$t3, 4
		multu	$t3, $t1		#	offset = 4*y
		mflo	$t3
		
		add		$t2, $t2, $t3	#	offset_address = board_address + 4*y
		lw		$t2, 0($t2)		#	word at offset_address

		li		$t3, 4
		multu	$t3, $t0		#	4*x
		mflo	$t3
		
		li		$t4, 28
		subu	$t3, $t4, $t3	#	28-4*x
		
		srl		$t3, $t2, $t3	#   1001xxxxxxxxxxxxxxxxxxxxxxxxxxxx becomes:
								#   00000000000000000000000000001001
		andi	$v0, $t3, 0xF		
getpieceD:              		# epilog
                                #   Return value is already in $v0        
        add     $sp,$fp,0       #   $sp = $fp + space_for_variables (==0)       
        lw      $fp,($sp)       #   Pop $fp
        add     $sp,$sp,4       #           
        lw      $ra,($sp)       #   Pop $ra
        add     $sp,$sp,4       #            
        jr      $ra             #   return to caller
##
# Get piece type for column and row
# $a0 - column x
# $a1 - row y
# $a2 - chess board location address
# $v0 - piece type
       .text
        .globl  getpiecetype
getpiecetype:
                                # prolog        
        sub     $sp,$sp,4       #   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       #   Push callers frame pointer
        sw      $fp,($sp)
        sub     $fp,$sp,0       #   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp         #   $sp = $fp

		jal		getpiece		#	e.g. 1101
		andi	$v0, $v0, 0x7	#	101 (Queen)
		
                         		# epilog
                                #   Return value is already in $v0        
        add     $sp,$fp,0       #   $sp = $fp + space_for_variables (==0)       
        lw      $fp,($sp)       #   Pop $fp
        add     $sp,$sp,4       #           
        lw      $ra,($sp)       #   Pop $ra
        add     $sp,$sp,4       #            
        jr      $ra             #   return to caller
##
# Get piece owner for column and row
# $a0 - column x
# $a1 - row y
# $a2 - chess board location address
# $v0 - black:0, white:1
       .text
        .globl  getpieceowner
getpieceowner:
                                				# prolog        
        sub     $sp,$sp,4       				#   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       				#   Push callers frame pointer
        sw      $fp,($sp)
        sub     $fp,$sp,0       				#   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp         				#   $sp = $fp

		jal		getpiece						#	e.g. 1101
		bne		$v0, 0x0,getpieceowner_owned	#	check if we have an empty square
		sub		$v0, $v0, 0x1					#   if so owner is -1 (no owner)
		j		getpieceowner_end
getpieceowner_owned:
		srl		$v0, $v0, 3						#	0001
		andi	$v0, $v0, 0x1					#	0001 (white)

getpieceowner_end:         						# epilog
                                				#   Return value is already in $v0        
        add     $sp,$fp,0       				#   $sp = $fp + space_for_variables (==0)       
        lw      $fp,($sp)       				#   Pop $fp
        add     $sp,$sp,4       				#           
        lw      $ra,($sp)       				#   Pop $ra
        add     $sp,$sp,4       				#            
        jr      $ra             				#   return to caller
##
# Draw piece for column and row
# $a0 - column x
# $a1 - row y
        .globl  drawpiece
drawpiece:
                                				# prolog        
        sub     $sp,$sp,4      					#   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       				#   Push callers frame pointer
        sw      $fp,($sp)
		sub		$sp,$sp,4						#	Push $s1
		sw		$s1,($sp)
        sub     $fp,$sp,0       				#   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp         				#   $sp = $fp

		move	$t0, $a0						#	Store temporarily
		move	$t1, $a1						#	Store temporarily

		add		$a0, $a0, $a1					#	Board spaces have an alternating pattern
		li		$a1, 2							#	Thus, depending on the position
		jal		mod								#	we apply the modulus to get that pattern
		move	$s1, $v0						#	Store it, because it will be used later
		beq		$s1, $zero, drawboardwhite		#	This space is white
		j		drawboardblack					#	This space is black

drawboardwhite:
		la		$a0, space						#	Just use a " " for white spaces
		jal		printstr						#	For example, | P |
		j		enddrawboardcolor				#                 ^
drawboardblack:
		la		$a0, dot						#	Use a ":" for black spaces
		jal		printstr						#	For example, |:P:|
												#                 ^
enddrawboardcolor:
		move	$a0, $t0
		move	$a1, $t1
		la		$a2, board
		jal		getpiece						#	Get the current piece in the space
		move	$t0, $v0
		
		beq		$t0, $zero, drawempty			#	If it is empty, then drawempty
		li		$t1, 9
		bge		$t0, $t1, drawwhite				#	If it is white, then drawwhite

drawblack:										#	It is black, drawblack
		li		$t1, 1
		subu	$v0, $v0, $t1					#	All pieces are offset from first black pawn
		
		la		$t1, bpawn						#	Get the first black pawn
		li		$t2, 2							#	ASCII chars are offset by 2 bytes
		multu	$t2, $v0
		mflo	$t2
		add		$t1, $t1, $t2					#	Calculate the offset
		
		move	$a0, $t1						#	And print that piece
		jal		printstr
		
		j		endpiece
drawwhite:
		li		$t1, 9
		subu	$v0, $v0, $t1					#	All pieces are offset from first white pawn

		la		$t1, wpawn						#	Get the first white pawn
		li		$t2, 2							#	ASCII chars are offset by 2 bytes
		multu	$t2, $v0
		mflo	$t2
		add		$t1, $t1, $t2					#	Calculate the offset

		move	$a0, $t1
		jal		printstr						#	And print that piece

		j		endpiece
drawempty:										#	Check if this space is black or white
		beq		$s1, $zero, drawboardwhiteempty	#	This space is white
		j		drawboardblackempty				#	This space is black

drawboardwhiteempty:
		la		$a0, space						#	Use a " " for white spaces
		jal		printstr						#	For example, |   |
		j		endpiece						#                  ^
drawboardblackempty:
		la		$a0, dot						#	Use a ":" for black spaces
		jal		printstr						#	For example, |:::|
												#                  ^
		
endpiece:
		beq		$s1, $zero, drawboardwhite3		#	This space is white
		j		drawboardblack3					#	This space is black

drawboardwhite3:
		la		$a0, space						#	Use a " " for white spaces
		jal		printstr						#	For example, |   |
		j		enddraw							#                   ^
drawboardblack3:
		la		$a0, dot						#	Use a ":" for white spaces
		jal		printstr						#	For example, |:::|
												#                   ^
		
enddraw:

                         						# epilog
                                				#   Return value is already in $v0        
        add     $sp,$fp,0       				#   $sp = $fp + space_for_variables (==0)       
        lw      $s1,($sp)       				#   Pop register $s1
        add     $sp,$sp,4       				#
        lw      $fp,($sp)       				#   Pop $fp
        add     $sp,$sp,4       				#           
        lw      $ra,($sp)       				#   Pop $ra
        add     $sp,$sp,4       				#            
        jr      $ra             				#   return to caller
###
#	Converts (column x, row y) to standard board location
#	e.g. (0,7) -> (A1)
#	$a0 : x
#	$a1 : y
       .text
        .globl  printboardloc
printboardloc:
                                # prolog        
        sub     $sp,$sp,4       #   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       #   Push callers frame pointer
        sw      $fp,($sp)
        sub     $fp,$sp,0       #   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp         #   $sp = $fp

		move	$t0, $a0		#	Store temporarily
		move	$t1, $a1		# 	Store temporarily

		li		$t2, 2
		multu	$t0, $t2		#	ASCII chars are stored offset by 2
		mflo	$t0

		la		$a0, A			#	Get the first ASCII char: A
		add		$a0, $a0, $t0	#	Offset from this char, see above declarations
		jal		printstr		#	Print the string

		li		$t3, 8			#	Our origin is top left. The standard is bottom-left
		sub		$t1, $t3, $t1	#	So subtract our row from 8
		move	$a0, $t1
		jal		printint		#	And print that
		
                         		# epilog
                                #   Return value is already in $v0        
        add     $sp,$fp,0       #   $sp = $fp + space_for_variables (==0)       
        lw      $fp,($sp)       #   Pop $fp
        add     $sp,$sp,4       #           
        lw      $ra,($sp)       #   Pop $ra
        add     $sp,$sp,4       #            
        jr      $ra             #   return to caller
###
#	Checks if a move is valid.
#	The move is from the selected location to the current location
#	(slocx, slocy) -> (locx, locy)
       .text
        .globl  checkmove
checkmove:
                                		# prolog        
        sub     $sp,$sp,4       		#   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       		#   Push callers frame pointer
        sw      $fp,($sp)
		sub		$sp,$sp,4				#
		sw		$s0,($sp)
		sub		$sp,$sp,4
		sw		$s1,($sp)
		sub		$sp,$sp,4
		sw		$s2,($sp)
		sub		$sp,$sp,4
		sw		$s3,($sp)
        sub     $fp,$sp,0       		#   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp        			#   $sp = $fp

		lw		$s0, slocx				# Load the selected location x
		lw		$s1, slocy				# Load the selected location y
		
		lw		$s2, locx				# Load the destination location x
		lw		$s3, locy				# Load the destination location y
		
		#move	$a0, $s2
		#move	$a1, $s3
		#la		$a2, board
		#jal		getpiece
		#bne		$v0, $zero, haderror	# If destination is occupied, then we have an error	
										# (to be modified)
		move	$a0, $s0
		move	$a1, $s1
		la		$a2, board
		jal		getpieceowner			# Get the owner of the piece to move
		move	$t4, $v0
		lw		$a0, turn
		bne		$t4, $a0, haderror		# If it is not the turn of the owner of the piece,
										# then an error has occured
		
		move	$a0, $s0
		move	$a1, $s1
		la		$a2, board
		jal		getpiece				# Get the piece to be moved
		move	$t5, $v0
		
		beq		$t5, 9, whitepawn		# If it is a white pawn go to whitepawn
		beq		$t5, 1, blackpawn		# If it is a black pawn go to blackpawn
		beq		$t5, 10, knight
		beq		$t5, 2, knight
		j		donecheck
#### Start Pawn ####
#### Start White Pawn ####		
whitepawn:
		beq		$s0, $s2, wnotcapture	# If move diagonally, then check if can capture
wtestdiag:
		move	$a2, $zero				# result = 0
		subu	$a0, $s0, $s2			# distance = slocx-locx
		li		$a1, -1					# test = -1
		seq		$a2, $a0, $a1 			# is distance == -1?
		li		$a1, 1
		beq		$a2, $a1, wtestdist		#  if (result == -1) then distance is one
				
		li		$a1, 1					# test = 1
		seq		$a2, $a0, $a2			# is distance == 1?
		
		li		$a1, 1
		beq		$a2, $a1, wtestdist		# if (result == 1) then distance is one
		j		wnotcapture
wtestdist:
		subu	$a0, $s1, $s3			# result = locy - slocy
		
		li		$a1, 1
		beq		$a0, $a1, checkopponent
		
		j		haderror
wnotcapture:
		subu	$t5, $s1, $s3			
		ble		$t5, $zero, donecheck	
		
		beq		$s1, 6, cantaketwo		# If pawn is in row 2 it can take two steps
		bgt		$t5, 1, haderror		# If not in row 2 and takes more than 1 step, then error
		j		readytomove
#### End White Pawn ####
#### Start Black Pawn ####		
blackpawn:
		beq		$s0, $s2, bnotcapture	# If move diagonally, then error (to be modified)
btestdiag:
		move	$a2, $zero				# result = 0
		subu	$a0, $s0, $s2			# distance = slocx-locx
		li		$a1, -1					# test = -1
		seq		$a2, $a0, $a1 			# is distance == -1?
		li		$a1, 1
		beq		$a2, $a1, btestdist			#  if (result == -1) then distance is one
		
		li		$a1, 1					# test = 1
		seq		$a2, $a0, $a2			# is distance == 1?

		li		$a1, 1
		beq		$a2, $a1, btestdist			# if (result == 1) then distance is one
		j		bnotcapture
btestdist:
		subu	$a0, $s3, $s1			# result = locy - slocy

		li		$a1, 1
		beq		$a0, $a1, checkopponent

		j		haderror

bnotcapture:
		subu	$t5, $s3, $s1
		ble		$t5, $zero, donecheck

		beq		$s1, 1, cantaketwo		# If pawn is in row 7 it can take two steps
		bgt		$t5, 1, haderror		# If not in row 7 and takes more than 1 step, then error
		j		readytomove
#### End Black Pawn ####
cantaketwo:
		bgt		$t5, 2, haderror		# Pawn can't take more than 2 steps
		j		readytomove
#### End Pawn ####
#### Start Knight ####
knight:
		#A: slocx + 2, slocy + 1
		#B: slocx + 2, slocy - 1
		#C: slocx - 2, slocy + 1
		#D: slocx - 2, slocy - 1
		#E: slocx + 1, slocy + 2
		#F: slocx - 1, slocy + 2
		#G: slocx + 1, slocy - 2
		#H: slocx - 1, slocy - 2
		
		subu	$a0, $s0, $s2			# First check if tried to move knight in same column
		beq		$a0, $zero, haderror
		
		subu	$a0, $s1, $s3			# Check if tried to move knight in same row
		beq		$a0, $zero, haderror
		
		#A
		addi	$a0, $s0, 2
		addi	$a1, $s1, 1

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#B
		addi	$a0, $s0, 2
		addi	$a1, $s1, -1

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#C
		addi	$a0, $s0, -2
		addi	$a1, $s1, 1

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#D
		addi	$a0, $s0, -2
		addi	$a1, $s1, -1

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#E
		addi	$a0, $s0, 1
		addi	$a1, $s1, 2

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#F
		addi	$a0, $s0, -1
		addi	$a1, $s1, 2

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#G
		addi	$a0, $s0, 1
		addi	$a1, $s1, -2

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty

		#H
		addi	$a0, $s0, -1
		addi	$a1, $s1, -2

		seq		$t0, $s2, $a0
		seq		$t1, $s3, $a1

		add		$t0, $t0, $t1
		beq		$t0, 2, checkempty
		
		j		haderror
checkempty:
		move	$a0, $s2
		move	$a1, $s3
		la		$a2, board
		jal		getpiece
		beq		$v0, $zero, readytomove
		j		checkopponent

#### End Knight ####
#### Start Bishop ####


#### End Bishop ####
readytomove:
		move	$a0, $s0
		move	$a1, $s1
		la		$a2, board
		jal		getpiece				# Get the piece to be moved
		
		move	$a2, $v0
		move	$a0, $s2
		move	$a1, $s3
		la		$a3, board
		jal		setpiece				# Move the piece to the destination
		
		move	$a0, $s0
		move	$a1, $s1
		move	$a2, $zero
		la		$a3, board
		jal		setpiece				# Make the original location empty
		
		j		donecheck
checkopponent:
		move	$a0, $s2
		move	$a1, $s3
		la		$a2, board
		jal		getpiece
		beq		$v0, $zero, haderror	# If destination is occupied, we check if it's an opponent

		move	$a0, $s2
		move	$a1, $s3
		la		$a2, board
		jal		getpieceowner

		la		$a2, turn
		bne		$v0, $a2, readytocapture

		j		haderror
readytocapture:
		move	$a0, $s0
		move	$a1, $s1
		la		$a2, board
		jal		getpiece				# Get the piece to be moved

		move	$a2, $v0
		move	$a0, $s2
		move	$a1, $s3
		la		$a3, board
		jal		setpiece				# Move the piece to the destination

		move	$a0, $s0
		move	$a1, $s1
		move	$a2, $zero
		la		$a3, board
		jal		setpiece				# Make the original location empty
		
		j		donecheck
donecheck:
		li		$a0, -1
		sw		$a0, slocx				# Reset selected location x
		sw		$a0, slocy				# Reset selected location y
		
		lw		$a0, turn				# Get the current turn
		nor		$a0, $a0, $a0			# 
		andi	$a0, $a0, 1				# Make it the other player's turn
		
		sw		$a0, turn				# Store it
		j		finished
haderror:
		sw		$s0, locx				# Reset
		sw		$s1, locy				# Reset

		li		$a0, -1
		sw		$a0, slocx				# Reset
		sw		$a0, slocy				# Reset
		li		$a0, 1					
		sw		$a0, error				# Set error flag
finished:

                         				# epilog
                                		#   Return value is already in $v0        
        add     $sp,$fp,0       		#   $sp = $fp + space_for_variables (==0)
		lw		$s3,($sp)
		add		$sp,$sp,4
		lw		$s2,($sp)
		add		$sp,$sp,4
		lw		$s1,($sp)
		add		$sp,$sp,4
		lw		$s0,($sp)
		add		$sp,$sp,4
        lw      $fp,($sp)       		#   Pop $fp
        add     $sp,$sp,4       		#           
        lw      $ra,($sp)       		#   Pop $ra
        add     $sp,$sp,4       		#            
        jr      $ra             		#   return to caller
###
# 
# Set piece for ($a0, $a1) to $a2
# $a3 is address of chess board
#
       .text
        .globl  setpiece
setpiece:
                                # prolog        
        sub     $sp,$sp,4       #   Push return address
        sw      $ra,($sp)
        sub     $sp,$sp,4       #   Push callers frame pointer
        sw      $fp,($sp)
        sub     $fp,$sp,0       #   $fp = $sp - space_for_variables (==0)
        move    $sp,$fp         #   $sp = $fp

		move	$t0, $a0		#	column x
		move	$t1, $a1		#	row y
		move	$t6, $a2

		move	$t2, $a3		#	get address to the chess board

		li		$t3, 4
		multu	$t3, $t1		#	offset = 4*y
		mflo	$t3

		add		$t2, $t2, $t3	#	offset_address = board_address + 4*y
		lw		$t5, 0($t2)		#	word at offset_address

		li		$t3, 4
		multu	$t3, $t0		# 	4*x
		mflo	$t3
		
		li		$t4, 28
		subu	$t3, $t4, $t3	# 	28-4*x
		
		li		$t7, 0xF		# 	For example, make A2 a white pawn
		sll		$t7, $t7, $t3	# 	11110000000000000000000000000000
		nor		$t7, $t7, $t7	# 	00001111111111111111111111111111
		and		$t5, $t7, $t5	# 	Mask the current row, which is:
								#	00001001100100000000000000000000
								#	With:
		sll		$t3, $t6, $t3	#   10010000000000000000000000000000
								#
		or		$t5, $t3, $t5	# 	OR them together to get
		sw		$t5, 0($t2)		#	10011001100100000000000000000000
								#	
								
								
                         		# epilog
                                #   Return value is already in $v0        
        add     $sp,$fp,0       #   $sp = $fp + space_for_variables (==0)       
        lw      $fp,($sp)       #   Pop $fp
        add     $sp,$sp,4       #           
        lw      $ra,($sp)       #   Pop $ra
        add     $sp,$sp,4       #            
        jr      $ra             #   return to caller
#############################################################################
#					Artificial Intelligence Area							#
#	AI is always going to be black, and will go to a mini-max depth of 5	#
#############################################################################

###
#	Get Value of Owned Pieces from a given board
#	a0 is boards location in memory
#	a1 is the player, 0 is black 1 is white
#	v0 returns the value of the board for black
#	P:10  K:35  B:37/39	R:50  Q:90  K:0
ai_get_owned_value:
	sub $sp,$sp,4				# backup variables
	sw $ra,0($sp)				#	ra
	sub $sp,$sp,4				#	
	sw $s0,0($sp)				#	s0
	sub $sp,$sp,4				#	
	sw $s1,0($sp)				#	s1
	sub $sp,$sp,4				#	
	sw $s2,0($sp)				#	s2
	sub $sp,$sp,4				#	
	sw $s3,0($sp)				#	s3
	sub $sp,$sp,4				#	
	sw $s4,0($sp)				#	s4
	sub $sp,$sp,4				#	
	sw $s5,0($sp)				#	s5
	
	li $s3,0					# total score counter
	li $s4,0					# dual bishop counter
	li $s0,8					# for loop max value
	li $s1,0					# row loop counter
	sll $s5,$a1,3				# player mask
ai_bval_rloop:					# for loop over rows
	li $s2,0					# column loop counter
ai_bval_cloop:					# for loop over columns
	move $a2,$a0				# get the piece at the current location
	move $a0,$s2
	move $a1,$s1
	jal getpiece
	move $a0,$a2
	addi $t0,$zero,1			# black pawn
	or $t0,$t0,$s5
	bne $v0,$t0,ai_bval_k
	addi $s3,$s3,10
	j ai_bval_O
ai_bval_k:
	addi $t0,$zero,2			# black knight
	or $t0,$t0,$s5
	bne $v0,$t0,ai_bval_b
	addi $s3,$s3,35
	j ai_bval_O
ai_bval_b:
	addi $t0,$zero,3			# black bishop
	or $t0,$t0,$s5
	bne $v0,$t0,ai_bval_r
	addi $s3,$s3,37
	addi $s4,$s4,1
	bne $s4,2,ai_bval_O
	addi $s3,$s3,4
	j ai_bval_O
ai_bval_r:
	addi $t0,$zero,4			# black rook
	bne $v0,$t0,ai_bval_q
	addi $s3,$s3,50
	j ai_bval_O
ai_bval_q:
	addi $t0,$zero,5			# black queen
	or $t0,$t0,$s5
	bne $v0,$t0,ai_bval_O
	addi $s3,$s3,90
ai_bval_O:
	addi $s2,$s2,1
	or $t0,$t0,$s5
	bne $s2,$s0,ai_bval_cloop
end_ai_bval_cloop:				# end for
	addi $s1,$s1,1
	or $t0,$t0,$s5
	bne $s1,$s0,ai_bval_rloop
end_ai_bval_rloop:				# end for
	move $v0,$s3	
								# retore variables
	lw $s5,0($sp)				#	s5
	add $sp,$sp,4				#
	lw $s4,0($sp)				#	s4
	add $sp,$sp,4				#
	lw $s3,0($sp)				#	s3
	add $sp,$sp,4				#
	lw $s2,0($sp)				#	s2
	add $sp,$sp,4				#
	lw $s1,0($sp)				#	s1
	add $sp,$sp,4				#
	lw $s0,0($sp)				#	s0
	add $sp,$sp,4				#
	lw $ra,0($sp)				#	ra
	add $sp,$sp,4				#
	jr $ra						# return to the caller

###
#	Generate Moves
#	generates all legal moves for piece at (a0,a1) on board at a2
#	v0 returns as number of moves
#	v1 returns as a pointer to a list of moves
ai_gen_moves:
								# save variables
	sub		$sp,$sp,4				#	ra
	sw		$ra,0($sp)				#
	sub		$sp,$sp,4				#	s0
	sw		$s0,0($sp)				#
	sub		$sp,$sp,4				#	s1
	sw		$s1,0($sp)				#
	sub		$sp,$sp,4				#	s2
	sw		$s2,0($sp)				#
	sub		$sp,$sp,4				#	s3
	sw		$s3,0($sp)				#
	sub		$sp,$sp,4				#	s4
	sw		$s4,0($sp)				#

	add		$s0,$zero,$a0
	add 	$s1,$zero,$a1
	add 	$s2,$zero,$a2
	add 	$s3,$zero,$zero
	add 	$s4,$zero,$zero
	jal		getpiece				# v0 now has the piece value
	and		$t0,$v0,0x7				# grab the piece's type
	and		$a3,$v0,0x8
	srl		$a3,$a3,3				# pass the player to the gen
	add		$t1,$zero,1
	bne		$t0,$t1,ai_gen_mr
	jal		ai_gen_pawn
	j		ai_gen_mO
ai_gen_mr:
	add		$t1,$zero,1
	bne		$t0,$t1,ai_gen_mn
	jal		ai_gen_rook
	j		ai_gen_mO
ai_gen_mn:
	add		$t1,$zero,1
	bne		$t0,$t1,ai_gen_mb
	jal		ai_gen_knight
	j		ai_gen_mO
ai_gen_mb:
	add		$t1,$zero,1
	bne		$t0,$t1,ai_gen_mq
	jal		ai_gen_bishop
	j		ai_gen_mO
ai_gen_mq:
	add		$t1,$zero,1
	bne		$t0,$t1,ai_gen_mk
	jal		ai_gen_queen
	j		ai_gen_mO
ai_gen_mk:
	add		$t1,$zero,1
	bne		$t0,$t1,ai_gen_mO
	jal		ai_gen_king
ai_gen_mO:
	add		$v0,$zero,$s3
	add		$v1,$zero,$s4
								# restore variables
	lw	$s4,0($sp)				#	s4
	add	$sp,$sp,4				#
	lw	$s3,0($sp)				#	s3
	add	$sp,$sp,4				#
	lw	$s2,0($sp)				#	s2
	add	$sp,$sp,4				#
	lw	$s1,0($sp)				#	s1
	add	$sp,$sp,4				#
	lw	$s0,0($sp)				#	s0
	add	$sp,$sp,4				#
	lw	$ra,0($sp)				#	ra
	add	$sp,$sp,4				#
	jr $ra

###
ai_gen_pawn:
	sub		$sp,$sp,4
	sw		$ra,0($sp)

	bne 	$a3,$zero,ai_gen_pw	# check if piece is black or white
	sub		$a0,$s0,1
	add		$a1,$s1,1
	add		$a2,$zero,$s2
	jal 	getpiece
	blt		$v0,0x8,ai_gen_pbd
	beq		$v0,-1,ai_gen_pbd
	add		$s3,$s3,1
ai_gen_pbd:
	add		$a0,$s0,1			# check the diagonal right for capture
	add		$a1,$s1,1
	add		$a2,$zero,$s2
	jal		getpiece			# get the piece, if it is >7 it is a white piece
	blt		$v0,0x8,ai_gen_pbm
	beq		$v0,-1,ai_gen_pbm
	add		$s3,$s3,1			# add one since we can take it
ai_gen_pbm:
	bne		$s1,1,ai_gen_pbs		# check if we can jump two
	add		$a1,$s1,2			# check if square is blocked
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pbs
	add		$s3,$s3,1			# if not we can move there, incr count
ai_gen_pbs:						# check if we can move one space ahead
	add		$a1,$s1,1
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pbg
	add		$s3,$s3,1
ai_gen_pbg:
	add		$t0,$zero,8
	mult	$s3,$t0				# get a block of memory for our moves
	mflo	$a0
	add		$v0,$zero,9			
	syscall
	add		$s4,$zero,$v0		# save this for return later
	sub		$a0,$s0,1			# check the diagonal right for capture
	add		$a1,$s1,1
	add		$a2,$zero,$s2
	jal		getpiece
	blt		$v0,0x8,ai_gen_pbgd
	beq		$v0,-1,ai_gen_pbgd
	sub		$t0,$s0,1			# add move to vector if it is valid
	add		$t1,$s1,1
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pbgd:
	add		$a0,$s0,1			# check the diagonal right for capture
	add		$a1,$s1,1
	add		$a2,$zero,$s2
	jal		getpiece			# get the piece, if it is >7 it is a white piece
	blt		$v0,0x8,ai_gen_pbgm
	beq		$v0,-1,ai_gen_pbgm
	add		$t0,$s0,1			# add move to vector
	add		$t1,$s1,1
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pbgm:
	bne		$s1,1,ai_gen_pbgs	# check if we can jump two
	add		$a1,$s1,2			# check if square is blocked
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pbgs
	add		$t0,$s0,0			# add move to vector
	add		$t1,$s1,2
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pbgs:					# check if we can move one space ahead
	add		$a1,$s1,1
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pawnO
	add		$t0,$s0,0			# add move to vector
	add		$t1,$s1,1
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
	j ai_gen_pawnO
ai_gen_pw:
	sub		$a0,$s0,1
	sub		$a1,$s1,1
	add		$a2,$zero,$s2
	jal 	getpiece
	bgt		$v0,0x7,ai_gen_pwd
	blt		$v0,0x1,ai_gen_pwd
	add		$s3,$s3,1
ai_gen_pwd:
	add		$a0,$s0,1			# check the diagonal right for capture
	sub		$a1,$s1,1
	add		$a2,$zero,$s2
	jal		getpiece			# get the piece, if it is >7 it is a white piece
	bgt		$v0,0x7,ai_gen_pwm
	blt		$v0,0x1,ai_gen_pwm
	add		$s3,$s3,1			# add one since we can take it
ai_gen_pwm:
	bne		$s1,6,ai_gen_pws		# check if we can jump two
	sub		$a1,$s1,2			# check if square is blocked
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pws
	add		$s3,$s3,1			# if not we can move there, incr count
ai_gen_pws:						# check if we can move one space ahead
	sub		$a1,$s1,1
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pwg
	add		$s3,$s3,1
ai_gen_pwg:
	add		$t0,$zero,8
	mult	$s3,$t0				# get a block of memory for our moves
	mflo	$a0
	add		$v0,$zero,9			
	syscall
	add		$s4,$zero,$v0		# save this for return later
	sub		$a0,$s0,1			# check the diagonal right for capture
	sub		$a1,$s1,1
	add		$a2,$zero,$s2
	jal		getpiece
	bgt		$v0,0x7,ai_gen_pwgd
	blt		$v0,0x1,ai_gen_pwgd
	sub		$t0,$s0,1			# add move to vector if it is valid
	sub		$t1,$s1,1
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pwgd:
	add		$a0,$s0,1			# check the diagonal right for capture
	sub		$a1,$s1,1
	add		$a2,$zero,$s2
	jal		getpiece			# get the piece, if it is >7 it is a white piece
	bgt		$v0,0x7,ai_gen_pwgm
	blt		$v0,0x1,ai_gen_pwgm
	add		$t0,$s0,1			# add move to vector
	sub		$t1,$s1,1
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pwgm:
	bne		$s1,6,ai_gen_pwgs	# check if we can jump two
	sub		$a1,$s1,2			# check if square is blocked
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pwgs
	add		$t0,$s0,0			# add move to vector
	sub		$t1,$s1,2
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pwgs:					# check if we can move one space ahead
	sub		$a1,$s1,1
	add		$a0,$zero,$s0
	add		$a2,$zero,$s2
	jal		getpiece
	bne		$v0,$zero,ai_gen_pawnO
	add		$t0,$s0,0			# add move to vector
	sub		$t1,$s1,1
	sw		$t0,0($s4)
	sw		$t1,4($s4)
	add		$s4,$s4,8
ai_gen_pawnO:
	add		$t0,$zero,8
	mult	$s3,$t0			
	mflo	$t0
	sub		$s4,$s4,$t0			# reset the location of the move vector
	lw	$ra,0($sp)				
	add	$sp,$sp,4				
	jr $ra

###
ai_gen_rook:
	jr $ra

###
ai_gen_knight:
	jr $ra

###
ai_gen_bishop:
	jr $ra

###
ai_gen_queen:
	jr $ra

###
ai_gen_king:
	jr $ra

###
#	Check-Check
#	umm...given a board and a player checks if they are checked
#	a0 is player
#	a1 is the board location in memory
#	v0 returns 1 if in checkmate, 0 if not
ai_check_check:
	sub 	$sp,$sp,4				# save variables
	sw		$ra,0($sp)
	sub 	$sp,$sp,4
	sw		$s0,0($sp)
	sub 	$sp,$sp,4
	sw		$s1,0($sp)
	sub 	$sp,$sp,4
	sw		$s2,0($sp)
	sub 	$sp,$sp,4
	sw		$s3,0($sp)
	sub 	$sp,$sp,4
	sw		$s4,0($sp)
	sub 	$sp,$sp,4
	sw		$s5,0($sp)
	sub 	$sp,$sp,4
	sw		$s6,0($sp)
	sub 	$sp,$sp,4
	sw		$s7,0($sp)
	
	move	$s0,$a0					# copy arguments to safe place
	move	$s1,$a1
	sll		$t0,$a0,3
	li		$s4,6
	or		$s4,$s4,$t0				# s4 now hold the king value for the player
	li		$s2,0				
ai_cc_rloop:						# loop over board to find king
	li		$s3,0
ai_cc_cloop:
	move	$a0,$s3
	move	$a1,$s2
	move	$a2,$s1
	jal		getpiece
	beq		$v0,$s4,ai_cc_eloop		# if we found the king, jump out
	add		$s3,$s3,1
	bne		$s3,8,ai_cc_cloop		# end column for
	add		$s2,$s2,1
	bne		$s2,8,ai_cc_rloop		# end row for
ai_cc_eloop:						# at king in s4 is at point (s3,s2)
	li		$s4,1
	xori	$s5,$s0,0x1
	sll		$s5,$s5,3
	or		$s5,$s5,2				# s5 now has value for enemy knight
	add		$a0,$s3,1				# check for knight at (+1,+2)
	add		$a1,$s2,2
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	add		$a0,$s3,2				# check for knight at (+2,+1)
	add		$a1,$s2,1
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	add		$a0,$s3,1				# check for knight at (+1,-2)
	sub		$a1,$s2,2
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	add		$a0,$s3,2				# check for knight at (+2,-1)
	sub		$a1,$s2,1
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	sub		$a0,$s3,1				# check for knight at (-1,+2)
	add		$a1,$s2,2
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	sub		$a0,$s3,2				# check for knight at (-2,+1)
	add		$a1,$s2,1
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	sub		$a0,$s3,1				# check for knight at (-1,-2)
	sub		$a1,$s2,2
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end
	sub		$a0,$s3,2				# check for knight at (-2,-1)
	sub		$a1,$s2,1
	add		$a2,$s1,0
	jal 	getpiece
	beq		$v0,$s5,ai_cc_end		# end knight checking
	
	li		$s5,1					# out loop counter
	li		$s6,0					# our blocking mask
	li		$s7,0					# our attacking mask
ai_cc_kloop:
	add		$a0,$s3,$s5				# check to the right
	add		$a1,$s2,$zero
	add		$a2,$s1,$zero
	jal 	getpiece
	#fill in here with the rest of the checks
	add		$s5,$s5,1
	bne		$s5,8,ai_cc_kloop
	li		$s4,0					# we didn't find an attacker
ai_cc_end:
	move	$v0,$s4
	lw		$s7,0($sp)				# restore variables
	add		$sp,$sp,4				
	lw		$s6,0($sp)				
	add		$sp,$sp,4				
	lw		$s5,0($sp)				
	add		$sp,$sp,4				
	lw		$s4,0($sp)				
	add		$sp,$sp,4				
	lw		$s3,0($sp)				
	add		$sp,$sp,4				
	lw		$s2,0($sp)				
	add		$sp,$sp,4				
	lw		$s1,0($sp)				
	add		$sp,$sp,4				
	lw		$s0,0($sp)				
	add		$sp,$sp,4				
	lw		$ra,0($sp)				
	add		$sp,$sp,4				
	jr 		$ra

###
#	Checkmate-Check
#	umm...given a board and a player checks if they are in checkmate
#	a0 is player
#	a1 is the board location
#	v0 returns 1 if in checkmate, 0 if not
ai_check_checkmate:
	sub 	$sp,$sp,4
	sw		$ra,0($sp)
	
	

	lw		$ra,0($sp)				
	add		$sp,$sp,4				
	jr 		$ra



#############################################################################
	.data
other_str:
	.asciiz "Non-interrupt exception\n"
no_recv_str:
	.asciiz "Receiver not ready\n"
no_trans_str:
	.asciiz "Transmitter not ready\n"
bad_int_str:
	.asciiz "Unknown interrupt\n"
